/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package percussiongenerator.deprecated;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.sound.midi.Instrument;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import percussiongenerator.model.TrackCollection;
import percussiongenerator.sound.IMidiPlayer;

/**
 *
 * @author Jannes Plyson
 */
public class MetronomePanel extends JPanel{

    JTextField textField;
    TrackCollection trackCollection;
    Instrument instrument;
    IMidiPlayer midiPlayer;
    public MetronomePanel(final TrackCollection trackCollection, final IMidiPlayer midiPlayer){
        this.midiPlayer = midiPlayer;
        instrument = null;
        this.trackCollection = trackCollection;
        this.setBorder(BorderFactory.createTitledBorder("Metronome"));
        this.setLayout(new BorderLayout());        
        Box labelBox = Box.createHorizontalBox();
        labelBox.add(Box.createGlue());
        JLabel label = new JLabel("BPM:");        
        labelBox.add(label);
        labelBox.add(Box.createGlue());
        this.add(labelBox,BorderLayout.NORTH);
        Box box = Box.createVerticalBox();
        textField = new JTextField();
        textField.setPreferredSize(new Dimension(40,25));
        textField.setMaximumSize(new Dimension(40,25));        
        box.add(textField);
        box.add(Box.createGlue());
        this.add(box,BorderLayout.CENTER);
        JButton btn = new JButton("Add");
        btn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try{
                    /*
                    int bpm = Integer.parseInt(textField.getText());
                    loadInstrument();
                    Track metronome = Track.createMetronome(bpm, instrument);
                    trackCollection.addTrack(metronome);
                    */
                }catch(Exception exc){

                }
            }
        });
        this.add(btn,BorderLayout.SOUTH);
        loadInstrument();
    }

    public void loadInstrument(){
        if(instrument == null && midiPlayer.isPlayerLoaded()){
            Instrument[] instruments = midiPlayer.getInstruments();            

            /*for(int i = 0; i < instruments.length; i++){
                System.out.println(instruments[i].toString());
            }*/
            int i = 0;            
            while(i< instruments.length && !instruments[i].toString().contains("Drumkit:")){
                i++;                
            }
            if(i < instruments.length){
                instrument = instruments[i];
            }else{
                instrument = instruments[0];
            }            
        }
    }

}
